package cn.antblog.bdi;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bonc.dataplatform.bdi.define.MailContent2Html;
import com.google.gson.Gson;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

/**
 * @author yangjy
 * @version 1.0
 * @description: 发送邮件
 * @date 2022/6/8 14:16
 */
public class SendEmail {

    private static final Logger log = LoggerFactory.getLogger(MailContent2Html.class);

    public Map<String, Object> content2Html(Map<String, Object> map) throws Exception {
        Map<String, Object> result = new HashMap<>(2);
        String emailHeader = map.get("emailHeader").toString().trim();
        String emailContent = map.get("emailContent").toString().trim();
        String emailTail = map.get("emailTail").toString().trim();
        try {
            String spliceHtml = spliceHtml(emailHeader, emailTail, emailContent);

            result.put("html", "spliceHtml");
        } catch (Exception e) {
            e.printStackTrace();
            log.error("html转换失败：" + e.getMessage());
            throw new Exception(e.getMessage());
        }
        return result;
    }


    public static String spliceHtml(String emailHeader, String emailTail, String emailContentList) {
        Gson gson = new Gson();
        List<Map<String, String>> list = gson.fromJson(emailContentList, List.class);
        JSONArray contentArray = JSONArray.parseArray(emailContentList);
        StringBuffer emailHtml = new StringBuffer();
        emailHtml.append(emailHeader);
        emailHtml.append("</br></br>");
        for (Object obj : contentArray) {
            JSONObject contentObj = (JSONObject) obj;
            String desc = (String) contentObj.get("desc");
            String content = (String) contentObj.get("content");
            String sort = (String) contentObj.get("sort");
            List<String> sortList = Arrays.asList(sort.split("\\|"));
            List<Map<String, String>> dealContentMapList = dealContent(content);
            emailHtml.append(desc);
            emailHtml.append("</br>");
            emailHtml.append("<table border=\"1\" bordercolor=\"#190707\" style=\"border-collapse:collapse;\">");
            emailHtml.append("<thead>");
            emailHtml.append("<tr>");
            for (String title : sortList) {
                emailHtml.append("<th style=\"word-break:keep-all;white-space:nowrap; \">" + title + "</th>");
            }
            emailHtml.append("</tr>");
            emailHtml.append("</thead>");
            emailHtml.append("<tbody>");
            for (Map<String, String> stringMap : dealContentMapList) {
                emailHtml.append("<tr>");
                for (String key : sortList) {
                    String val = stringMap.get(key.trim());
                    emailHtml.append("<td>" + val + "</td>");
                }
                emailHtml.append("</tr>");
            }
            emailHtml.append("</tbody>");
            emailHtml.append("</table>");
            emailHtml.append("</br>");
        }
        emailHtml.append("<p style = \"color: #9F9F9F\">--------------------------------------------------------------------------------</p>");
        emailHtml.append(emailTail);
        System.out.println("emailHtml = " + emailHtml);
        return emailHtml.toString();
    }

    public static List<Map<String, String>> dealContent(String originContent) {
        if (null != originContent && !originContent.equals("")) {
            List<Map<String, String>> mapListResult = new ArrayList<>();
            //originContent = "[{办理人2=sdsc-miaoxl7, 服务号码2=053000035531, 账期2=20220428, 产品编码2=52747705}, {办理人2=sd-zhuhc, 服务号码2=053000037427, 账期2=20220428, 产品编码2=51003293}, {办理人2=sd-konggy, 服务号码2=053000057987, 账期2=20220428, 产品编码2=52747700}, {办理人2=sdsc-duanqq, 服务号码2=053000279520, 账期2=20220428, 产品编码2=51003293}]";
            //掐头去尾：
            String content = originContent.replace("[{", "").replace("}]", "");
            //根据}, {分割
            //commaSignContentList：办理人2=sdsc-miaoxl7, 服务号码2=053000035531, 账期2=20220428, 产品编码2=52747705
            List<String> commaSignContentList = Arrays.asList(content.split("}, \\{"));
            for (String commaSignContent : commaSignContentList) {
                //根据,分割
                List<String> equalSignContentList = Arrays.asList(commaSignContent.split(","));
                Map<String, String> hashMap = new HashMap<>(16);
                for (String equalSignContent : equalSignContentList) {
                    //根据=分割
                    String[] keyValArr = equalSignContent.split("=");
                    hashMap.put(keyValArr[0].trim(), keyValArr[1].trim());
                }
                mapListResult.add(hashMap);
            }
            return mapListResult;
        }
        log.error("originContent邮件内容为空！");
        return null;
    }

    public static void main(String[] args) {
        String emailHeader = "您好！";
        String emailTail = "尾";
        String emailContentList = "[{\n" +
                "\t\"key\": \"0\",\n" +
                "\t\"content\": \"[{账期=20220428, 服务号码=053000035531, 办理人=sdsc-miaoxl7, 产品编码=52747705}, {账期=20220428, 服务号码=053000037427, 办理人=sd-zhuhc, 产品编码=51003293}, {账期=20220428, 服务号码=053000057987, 办理人=sd-konggy, 产品编码=52747700}, {账期=20220428, 服务号码=053000279520, 办理人=sdsc-duanqq, 产品编码=51003293}, {账期=20220428, 服务号码=053000281996, 办理人=sd-xiaorr, 产品编码=52747700}, {账期=20220428, 服务号码=053000295406, 办理人=sd-liuhd3, 产品编码=52747700}, {账期=20220428, 服务号码=053000327386, 办理人=sdsc-miaoxl7, 产品编码=52747700}, {账期=20220428, 服务号码=053000328411, 办理人=sd-zhangqh6, 产品编码=90309653}, {账期=20220428, 服务号码=053000345217, 办理人=sdsc-wangyy277, 产品编码=91021875}, {账期=20220428, 服务号码=053000368502, 办理人=sdsc-miaoxl7, 产品编码=52747700}]\",\n" +
                "\t\"desc\": \"数据1\",\n" +
                "\t\"sort\": \"账期|服务号码|办理人|产品编码\"\n" +
                "}, {\n" +
                "\t\"key\": \"1\",\n" +
                "\t\"desc\": \"数据2\",\n" +
                "\t\"content\": \"[{办理人2=sdsc-miaoxl7, 服务号码2=053000035531, 账期2=20220428, 产品编码2=52747705}, {办理人2=sd-zhuhc, 服务号码2=053000037427, 账期2=20220428, 产品编码2=51003293}, {办理人2=sd-konggy, 服务号码2=053000057987, 账期2=20220428, 产品编码2=52747700}, {办理人2=sdsc-duanqq, 服务号码2=053000279520, 账期2=20220428, 产品编码2=51003293}]\",\n" +
                "\t\"sort\": \"账期2|服务号码2|办理人2|产品编码2\"\n" +
                "}]";
        Gson gson = new Gson();
        List<Map<String, String>> list = gson.fromJson(emailContentList, List.class);

        System.out.println("list = " + list);

        //spliceHtml(emailHeader, emailTail, emailContentList);
    }
}
